package com.ilearn.autocomplie.code;

import com.ilearn.autocomplie.entities.EntityModel;
import com.ilearn.autocomplie.support.JdkCompiler;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
 * @Project: seb-core
 * @Description:
 * @Version 1.0
 * @Throws SystemException:
 * @Author: <li>2019/1/13 lyndon Create 1.0
 * @Copyright (c)2018-2018 中央结算公司，版权所有
 * @Modified By:
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AutoCompileLoader {

    private String tableName;

    private List<String> columnNames;

    private List<String> columnTypes;

    private static ClassLoader findClassLoader() {
        return AutoCompileLoader.class.getClassLoader();
    }

    public Class<?> proxyLoader(Class<?> type, String tableName,  List<String> columnNames, List<String> columnTypes) {
        final GenerateCode generateCode = new GenerateCode();

        String code = generateCode.createCode(type, tableName, columnNames, columnTypes);

        System.out.println(code);

        // 采用JDK编译器
        final JdkCompiler jdkCompiler = new JdkCompiler();
        final Class<?> aClass = jdkCompiler.compile(code, findClassLoader());

        //System.out.println(aObj.getName());

        return aClass;
    }

    public static void main(String[] args) {

        String tableName = "members";

        List<String> columnNames = Arrays.asList("id", "name", "age");

        List<String> columnTypes = Arrays.asList("String", "String", "Integer");

        final AutoCompileLoader autoCompileLoader = new AutoCompileLoader();

        /*final GenerateCode generateCode = new GenerateCode();

        String code = generateCode.createCode(autoCompileLoader.type, tableName, columnNames, columnTypes);

        System.out.println(code);

        // 采用JDK编译器
        final JdkCompiler jdkCompiler = new JdkCompiler();
        final Class<?> aObj = jdkCompiler.compile(code, findClassLoader());

        // 采用Javassist编译器
        *//*final JavassistCompiler javassistCompiler = new JavassistCompiler();
        final Class<?> aObj = javassistCompiler.compile(code, findClassLoader());*//*

        System.out.println(aObj.getName());*/

        final Class<?> aClass = autoCompileLoader.proxyLoader(EntityModel.class, tableName, columnNames, columnTypes);

        System.out.println(aClass.getName());

        for (Field field : aClass.getFields()) {
            System.out.println(field.getName());
        }
        System.out.println("============== It's methods ===============");
        for (Method method : aClass.getMethods()) {
            System.out.println(method.getName());
        }

        // 生成代理类
        try {
            EntityModel entityModel = (EntityModel) aClass.newInstance();
            System.out.println(entityModel.getClass().getName());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }


    }

}
